<?php
require_once "MyRabbitMq.php";

use MyRabbitMq\MyRabbitMq;
use PhpAmqpLib\Message\AMQPMessage;
use PhpAmqpLib\Exchange\AMQPExchangeType;

//这里的q1队列和这里的ex1交换机之前已经定义
$exchangeName = "ex1";
$queueName = "q1";
//位置的路由键
$routeKey = "key3";

$rabbit = new MyRabbitMq();
$ch = $rabbit->getChannel();

//发送成功的数据
$ch->set_return_listener(function ($replyCode, $replyText, $exchangeName, $routeKey, AMQPMessage $message) {
    echo $replyCode . PHP_EOL . $replyText . PHP_EOL . $exchangeName . PHP_EOL . $routeKey . PHP_EOL . $message->body . PHP_EOL;
});
$rabbit->setConfirm();
$rabbit->setAckHandler(function (AMQPMessage $message){
    echo  "发送成功".$message->body;
});
$rabbit->setNAckHandler(function (AMQPMessage $message){
    echo  "发送失败".$message->body;
});

$properties = ['content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT];
$msgBody = "123";
$rabbit->sendMsg($msgBody, $properties, $exchangeName, $routeKey, true);
$rabbit->waitForPendingAckReturns(1);


$rabbit->close();